package development;

import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.component.Event;
import state.component.EventListener;
import state.component.MultiTab;
import state.font.FontManager;
import state.font.ImageFont;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import development.group.FrmGroupKetBanMangXHChat;
import development.group.FrmGroupListTableFreeUserChat;
import development.group.FrmGroupMenu;
import development.socket.SocketClientUtil;

public class FrmChat extends GameForm implements EventListener {
  private GameImage bgPopupImage;
  private ImageFont text8;
  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmChat();
    }
    return singleton;
  }
  
  public void init(Hashtable parameters) {
    bgPopupImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "bg_popup.png");
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
  }
  
  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    g.drawImage(bgPopupImage, Constants.SCREEN_WIDTH / 2, Constants.SCREEN_HEIGHT / 2, Constants.CENTER_ANCHOR);
    text8.drawString2(g, "Chức năng đang hoàn thiện", GameColor.WHITE_CODE, Constants.SCREEN_WIDTH / 2, Constants.SCREEN_HEIGHT / 2, Constants.CENTER_ANCHOR);
  }
  
  public void onActionPerform(Event event) {
    if (MultiTab.OPEN_TAB_EVENT.equals(event.getAction())) {
      if (parentGroup instanceof FrmGroupMenu) {
        DrawUtils.getBottomBar(FrmGroupMenu.instance().getManager()).setRightLabel("Trở về");
      } else if (parentGroup instanceof FrmGroupKetBanMangXHChat) {
        DrawUtils.getBottomBar(FrmGroupKetBanMangXHChat.instance().getManager()).setRightLabel("Trở về");
      }  else if (parentGroup instanceof FrmGroupListTableFreeUserChat) {
        DrawUtils.getBottomBar(FrmGroupListTableFreeUserChat.instance().getManager()).setRightLabel("Trở về");
      }
      return;
    }
  }
  
  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.LEFT:
      if (parentGroup instanceof FrmGroupMenu) {
        FrmGroupMenu frmMenu = (FrmGroupMenu) FrmGroupMenu.instance();
        if (frmMenu.getMultiTab() != null) {
          frmMenu.getMultiTab().previousTab();
        }
      } else if (parentGroup instanceof FrmGroupKetBanMangXHChat) {
        FrmGroupKetBanMangXHChat.setTab(1);
      } else if (parentGroup instanceof FrmGroupListTableFreeUserChat) {
        FrmGroupListTableFreeUserChat.setTab(1);
      }
      break;
    case Key.SOFT_RIGHT:
      if (parentGroup != null) {
        if (parentGroup instanceof FrmGroupKetBanMangXHChat) {
          parentGroup.goBack();
        } else if (parentGroup instanceof FrmGroupListTableFreeUserChat) {
          SocketClientUtil.leaveRoomRequest();
          GameGlobal.nextState(FrmListRoom.instance(), null);
        }
      }
      break;
    }
  }
  
  public void destroy() {
    bgPopupImage = null;
  }
}
